package com.mstore.dao;

import java.util.List;

import org.apache.commons.lang.StringUtils;

import com.mstore.pojo.AccountAlipayGoods;

public class AccountAlipayGoodsDao extends SuperDao {

	/**
	 * 保存
	 * * @param accountAlipayGoods
	 */
	public void addAccountAlipayGoods(AccountAlipayGoods accountAlipayGoods) {
		super.save(accountAlipayGoods);
	}
	
	/**
	 * 更新
	 * * @param accountAlipayGoods
	 */
	public void updateAccountAlipayGoods(AccountAlipayGoods accountAlipayGoods) {
		String separate = " set ";
		StringBuilder sb = new StringBuilder();
		sb.append("update ubcenter.b_account_alipay_goods");
		
		Integer amount = accountAlipayGoods.getAmount();
		if (amount != null) {
			sb.append(separate);
			sb.append("amount = ");
			sb.append(amount);
			separate = ", ";
		}
		
		String beginDate = accountAlipayGoods.getBeginDate();
		if (StringUtils.isNotBlank(beginDate)) {
			sb.append(separate);
			sb.append("begin_date = '");
			sb.append(beginDate);
			sb.append("'");
			separate = ", ";
		}
		
		String endDate = accountAlipayGoods.getEndDate();
		if (StringUtils.isNotBlank(endDate)) {
			sb.append(separate);
			sb.append("end_date = '");
			sb.append(beginDate);
			sb.append("'");
			separate = ", ";
		}
		
		sb.append(" where apay_id = ");
		sb.append(accountAlipayGoods.getId());
		
		super.executeUpdate(sb.toString());
	}
	
	/**
	 * 通过企业id、goodType查询企业购买的商品
	 * 查询剩余使用次数或是否还在有效时间内
	 */
	public Object getAccountAlipayGoodsByEaIdAndGoodtype(Long eaId, String goodType) {
		String sql = "from AccountAlipayGoods where eaId = ? and goodType = ?";
		List<Object> list = getObject(sql, new Object[]{ eaId, goodType });
		if (list.isEmpty()) {
			return null;
		}
		
		return list.get(0);
	}
	
	/**
	 * 查询企业购买的商品
	 * * @param eaId
	 */
	public List<Object> getAccountAlipayGoodsByEaId(Long eaId) {
		String sql = "from AccountAlipayGoods where eaId = ?";
		return getObject(sql, new Object[]{ eaId });
	}
	
}
